home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c04 / MultiDimArray.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  4.4 KB  |  114 lines

  1. //: MultiDimArray.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Creating multidimensional arrays.
  50. import java.util.*;
  51.  
  52. public class MultiDimArray {
  53.   static Random rand = new Random();
  54.   static int pRand(int mod) {
  55.     return Math.abs(rand.nextInt()) % mod + 1;
  56.   }
  57.   public static void main(String[] args) {
  58.     int[][] a1 = {
  59.       { 1, 2, 3, },
  60.       { 4, 5, 6, },
  61.     };
  62.     for(int i = 0; i < a1.length; i++)
  63.       for(int j = 0; j < a1[i].length; j++)
  64.         prt("a1[" + i + "][" + j +
  65.             "] = " + a1[i][j]);
  66.     // 3-D array with fixed length:
  67.     int[][][] a2 = new int[2][2][4];
  68.     for(int i = 0; i < a2.length; i++)
  69.       for(int j = 0; j < a2[i].length; j++)
  70.         for(int k = 0; k < a2[i][j].length;
  71.             k++)
  72.           prt("a2[" + i + "][" +
  73.               j + "][" + k +
  74.               "] = " + a2[i][j][k]);
  75.     // 3-D array with varied-length vectors:
  76.     int[][][] a3 = new int[pRand(7)][][];
  77.     for(int i = 0; i < a3.length; i++) {
  78.       a3[i] = new int[pRand(5)][];
  79.       for(int j = 0; j < a3[i].length; j++)
  80.         a3[i][j] = new int[pRand(5)];
  81.     }
  82.     for(int i = 0; i < a3.length; i++)
  83.       for(int j = 0; j < a3[i].length; j++)
  84.         for(int k = 0; k < a3[i][j].length;
  85.             k++)
  86.           prt("a3[" + i + "][" +
  87.               j + "][" + k +
  88.               "] = " + a3[i][j][k]);
  89.     // Array of non-primitive objects:
  90.     Integer[][] a4 = {
  91.       { new Integer(1), new Integer(2)},
  92.       { new Integer(3), new Integer(4)},
  93.       { new Integer(5), new Integer(6)},
  94.     };
  95.     for(int i = 0; i < a4.length; i++)
  96.       for(int j = 0; j < a4[i].length; j++)
  97.         prt("a4[" + i + "][" + j +
  98.             "] = " + a4[i][j]);
  99.     Integer[][] a5;
  100.     a5 = new Integer[3][];
  101.     for(int i = 0; i < a5.length; i++) {
  102.       a5[i] = new Integer[3];
  103.       for(int j = 0; j < a5[i].length; j++)
  104.         a5[i][j] = new Integer(i*j);
  105.     }
  106.     for(int i = 0; i < a5.length; i++)
  107.       for(int j = 0; j < a5[i].length; j++)
  108.         prt("a5[" + i + "][" + j +
  109.             "] = " + a5[i][j]);
  110.   }
  111.   static void prt(String s) {
  112.     System.out.println(s);
  113.   }
  114. } ///:~